﻿// ______________________________________________________________
//
// Noise overlay animation class - Liam O'Donnell
// Creates an overlay like film grain or TV noise
// ______________________________________________________________
//

// ______________________________________________________________
//
// USAGE:
// - Create/attach a movieclip that's an instance of NoiseOverlay
// - You can alternatively use the static method 'create'
// ______________________________________________________________
//

import flash.geom.Rectangle;
import flash.display.BitmapData;
import com.spikything.utils.SpikyTools;

class com.spikything.anim.NoiseOverlay extends MovieClip {

	// ______________________________________________________
	//
	// CONSTANTS
	// ______________________________________________________
	//

	private var bmp 		:BitmapData;
	private var noiseMC 	:MovieClip;
	private var subDivision :Number;
	private var width 		:Number;
	private var height 		:Number;

	public 	var monochrome 	:Boolean;

	// ______________________________________________________
	//
	// CONSTRUCTOR
	// ______________________________________________________
	//

	function NoiseOverlay () {}

	// ______________________________________________________
	//
	// PUBLIC METHODS
	// ______________________________________________________
	//

	public function onLoad ():Void {
		init();
	}

	// ______________________________________________________
	//
	// PRIVATE METHODS
	// ______________________________________________________
	//

	private function init ():Void {
		bmp = new BitmapData(width/subDivision, height/subDivision, false, 0x000000);
		_xscale = _yscale = 100*subDivision;
		attachBitmap(bmp,0);
		onEnterFrame = update;
	}

	private function update ():Void {
		if (monochrome) {
			bmp.noise(random(256),0,255,0,true);
		} else {
			bmp.noise(random(256));
		}
	}

	public function onUnload() {
		bmp.fillRect(new Rectangle(0,0,bmp.width,bmp.height), 0x000000);
		bmp.dispose();
	}

	// ______________________________________________________
	//
	// STATIC METHODS
	// ______________________________________________________
	//

	public static function create (_base:MovieClip,_initObj:Object,_depth:Number):NoiseOverlay {
		return SpikyTools.createClass(_base,NoiseOverlay,_initObj,null,_depth);
	}

}